
#include "mqtt_json.h"

volatile MQTTClient_deliveryToken deliveredtoken;
MQTTClient client,aiotclient;


void delivered(void *context, MQTTClient_deliveryToken dt)
{
    //  printf("Message with token value %d delivery confirmed\n", dt);
    deliveredtoken = dt;
}
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    cJSON *payloadptr = cJSON_Parse((char *)message->payload);
    // 开启线程防止MQTT通信阻塞
    pthread_t thread;
    pthread_create(&thread, NULL, command_handler_thread, payloadptr);
    pthread_detach(thread);
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}
void connlost(void *context, char *cause)
{
    printf("\nConnection lost\n");
    printf("     cause: %s\n", cause);
}

// MQTT客户端初始化并订阅主题
void MQTT_init()
{
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    int rc;
    MQTTClient_create(&client, ADDRESS, CLIENTID_P,
                      MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect qt, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("MQTT_INIT successful\n");
    }
    MQTTClient_subscribe(client, TOPIC_S, QOS);
}

// MQTT客户端初始化并订阅主题
void MQTT_aiot_init()
{
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    int rc;
    MQTTClient_create(&aiotclient, "mqtt.yyzlab.com.cn", CLIENTID_P,
                      MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    

    if ((rc = MQTTClient_connect(aiotclient, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connectqiot, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("MQTT_INIT successful\n");
    }

}


// 向主题发送内容
void publishToQt(const char *payload)
{
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc1, rc2;

    pubmsg.payload = (void *)payload;
    pubmsg.payloadlen = (int)strlen(payload);
    pubmsg.qos = QOS;
    pubmsg.retained = 0; // 非保留消息

    if ((rc1 = MQTTClient_publishMessage(client, TOPIC_P, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to publish message, return code %d\n", rc1);
        return;
    }
    rc1 = MQTTClient_waitForCompletion(client, token, 1000L); // 等待1秒发布完成
    rc2 = MQTTClient_waitForCompletion(client, token, 1000L); // 等待1秒发布完成
    if (rc1 != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to wait for completion qt, return code %d\n", rc1);
    }
    else
    {
        // printf("Message with delivery token %d delivered\n", token);
    }
}
void publishToAito(const char *payload)
{
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc1;

    pubmsg.payload = (void *)payload;
    pubmsg.payloadlen = (int)strlen(payload);
    pubmsg.qos = QOS;
    pubmsg.retained = 0; // 非保留消息
    if ((rc1 = MQTTClient_publishMessage(aiotclient, "aito", &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to publish message, return code %d\n", rc1);
        return;
    }
    rc1 = MQTTClient_waitForCompletion(aiotclient, token, 1000L); // 等待1秒发布完成
    if (rc1 != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to wait for completion aito, return code %d\n", rc1);
    }
    else
    {
        // printf("Message with delivery token %d delivered\n", token);
    }
}
